package com.ad.pet.app.address;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.address.Address;
import com.ad.pet.models.remote.address.Province;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/6/26.
 */
@PerActivity
public class AddressPresenter implements AddressContract.Presenter {

    private ApiService mApiService;
    private AddressContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public AddressPresenter(ApiService apiService,
                            Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void onCreate(AddressContract.View view) {
        this.mView = view;
    }


    @Override
    public void getAddressList() {
        mRxDisposable.add(mApiService.address_list().subscribe(new AppSubscriber<List<Address>>(mView) {
            @Override
            public void next(List<Address> listCommList) {
                mView.getAddresssListSuccess(listCommList);
            }
        }));
    }

    @Override
    public void updataAddress(String address_id,String consigner,String mobile,
                              String province,String city,String district,String address,String label,
                              int defaults) {
        mRxDisposable.add(mApiService.address_update(address_id,consigner,mobile,
                province,city,district,address, label,
         defaults)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.getUpdateAddressSuccess();
                    }
                }));
    }

    @Override
    public void addAddress(String consigner, String mobile, String province, String city, String district, String address, String label, int defaults) {
        mRxDisposable.add(mApiService.address_add( consigner,  mobile,  province,  city,  district, address,  label,defaults)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.getUpdateAddressSuccess();
                    }
                }));
    }


    @Override
    public void delAddress(String aid) {
        mRxDisposable.add(mApiService.address_del(aid)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.delAddressSuccess();
                    }
                }));
    }

    @Override
    public void getCountryProvence() {
        mRxDisposable.add(mApiService.getProvinceCity()
                .subscribe(new AppSubscriber<List<Province>>(mView) {
                    @Override
                    public void next(List<Province> province) {
                        mView.getCountryProvenceSuccess(province);
                    }
                }));
    }
}
